<?php

/**
 * accueil actions.
 *
 * @package    SdCommerce
 * @subpackage accueil
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class accueilActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

//      $this->forward404Unless($manager = Doctrine_Core::getTable('sfGuardUser')->find(array($request->getParameter('id'))), sprintf('Object manager does not exist (%s).', $request->getParameter('id')));
//      $this->managers = Doctrine_Core::getTable('sfGuardUser')->findById($request->getParameter('id'));
    }

    public function executeHideNotification(sfWebRequest $request) {
        $id = $request->getParameter('id');
        $notification = NotificationTable::getInstance()->find($id);
        $notification->setIsShown(true);
        $notification->save();
        return $this->renderText('');
    }

    public function executeGetNotificationAdmin() {
        $notifications = NotificationTable::getInstance()->getNotificationAdmin();
        $result = array();
        $result['nb'] = count($notifications);
        $notys = array();
        if (count($notifications) > 0)
            foreach ($notifications as $k => $notification) {
                $notys[$k]['contenu'] = $notification->getContenu();
                $notys[$k]['show'] = $notification->getIsShown();
                $notys[$k]['id'] = $notification->getId();
                $notys[$k]['time'] = dates::dateHeurForm($notification->getCreatedAt());
            }
        $result['contenu'] = $notys;
        return $this->renderText(json_encode($result));
    }

    public function executeShowNotification(sfWebRequest $request) {
        $id = $request->getParameter('id');
        $notification = NotificationTable::getInstance()->find($id);
        $result = $notification->getContenu();
        $bon_entre = $notification->getBonEntre();
        $bon_livraison_fournisseur = $notification->getBonLivraisonFournisseur();
        $lignes_bon_entre = $bon_entre->getLigneBonEntre();
        $lignes_bon_livraison_fournisseur = $bon_livraison_fournisseur->getLignes();
        foreach ($lignes_bon_entre as $k => $ligne_bon_entre) {
            $result_be[$k] = $this->getQunatityDesignationForBE($ligne_bon_entre);
        }
        foreach ($lignes_bon_livraison_fournisseur as $k => $ligne_bon_livraison_fournisseur) {
            $result_bl[$k] = $this->getQunatityDesignationForBL($ligne_bon_livraison_fournisseur);
        }
        $tableau = array();
        $ii = 0;
        $i = count($result_be) - 1;
        $j = count($result_bl) - 1;
        while ($i >= 0) {
            while ($j >= 0) {
                if ($result_be[$i]['designation'] == $result_bl[$j]['designation']) {
                    if ($result_be[$i]['qte'] != $result_bl[$j]['qte']) {
                        $tableau[$ii]['designation'] = $result_be[$i]['designation'];
                        $tableau[$ii]['qte_be'] = $result_be[$i]['qte'];
                        $tableau[$ii]['qte_bl'] = $result_bl[$j]['qte'];
                        $ii++;
                    }
                    unset($result_bl[$j]);
                    unset($result_be[$i]);
                    $result_be = array_values($result_be);
                    $result_bl = array_values($result_bl);
                    break;
                }
                $j--;
            }
            $j = count($result_bl) - 1;
            $i--;
        }

        if (count($result_be) > 0) {
            for ($i = 0; $i < count($result_be); $i++) {
                $tableau[$ii]['designation'] = $result_be[$i]['designation'];
                $tableau[$ii]['qte_be'] = $result_be[$i]['qte'];
                $tableau[$ii]['qte_bl'] = 0;
                $ii++;
            }
        }
        if (count($result_bl) > 0) {
            for ($i = 0; $i < count($result_be); $i++) {
                $tableau[$ii]['designation'] = $result_bl[$i]['designation'];
                $tableau[$ii]['qte_be'] = 0;
                $tableau[$ii]['qte_bl'] = $result_bl[$i]['qte'];
                $ii++;
            }
        }
        $notification->setIsHidden(true);
        $notification->save();
        return $this->renderPartial('listeEcart', array("tableau" => $tableau));
    }

    protected function getQunatityDesignationForBE($ligne) {
        $result = array();
        $result['qte'] = $ligne->getQuantiteMvt();
        $result['designation'] = $ligne->getDesignation();
        return $result;
    }

    protected function getQunatityDesignationForBL($ligne) {
        $result = array();
        $result['qte'] = $ligne->getQuantite();
        $result['designation'] = $ligne->getDesignation();
        return $result;
    }

}
